package com.hd.common.taobao.notify;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hd.assistant.biz.manager.ordermining.OrderMiningManager;
import com.hd.assistant.biz.manager.ordermining.concurrent.MessageInsertDBTask;
import com.taobao.api.internal.stream.message.TopCometMessageListener;

public class CommonTopCometMessageListener implements TopCometMessageListener{
	private final static Logger log = LoggerFactory
			.getLogger(CommonTopCometMessageListener.class);
	private Integer TASK_POOP_SIZE = 4;
	private ThreadPoolExecutor threadPool = new ThreadPoolExecutor(TASK_POOP_SIZE, TASK_POOP_SIZE,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>());;
	
	public CommonTopCometMessageListener() {
		// TODO Auto-generated constructor stub
	}
	
	public CommonTopCometMessageListener(OrderMiningManager orderMiningManager) {
		super();
		this.orderMiningManager = orderMiningManager;
	}

	OrderMiningManager orderMiningManager;
	public OrderMiningManager getOrderMiningManager() {
		return orderMiningManager;
	}

	public void setOrderMiningManager(OrderMiningManager orderMiningManager) {
		this.orderMiningManager = orderMiningManager;
	}

	@Override
	public void onConnectMsg(String message) {
		System.out.println(message);
		try{
			MessageInsertDBTask task = new MessageInsertDBTask(orderMiningManager, message);
		this.threadPool.execute(task);
		}catch(Exception e){
			log.error("message listener error:",e);
		}
		
	}

	@Override
	public void onHeartBeat() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onReceiveMsg(String message) {
		System.out.println(message);
		try{
			MessageInsertDBTask task = new MessageInsertDBTask(orderMiningManager, message);
		this.threadPool.execute(task);
		}catch(Exception e){
			log.error("message listener error:",e);
		}
	}

	@Override
	public void onDiscardMsg(String message) {
		System.out.println("onDiscardMsg--"+message);
		try{
			MessageInsertDBTask task = new MessageInsertDBTask(orderMiningManager, message);
		this.threadPool.execute(task);
		}catch(Exception e){
			log.error("message listener error:",e);
		}
	}

	@Override
	public void onServerUpgrade(String message) {
		System.out.println(message);
		try{
			MessageInsertDBTask task = new MessageInsertDBTask(orderMiningManager, message);
		this.threadPool.execute(task);
		}catch(Exception e){
			log.error("message listener error:",e);
		}
	}

	@Override
	public void onServerRehash() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onServerKickOff() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onClientKickOff() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onOtherMsg(String message) {
		System.out.println(message);
		try{
			MessageInsertDBTask task = new MessageInsertDBTask(orderMiningManager, message);
		this.threadPool.execute(task);
		}catch(Exception e){
			log.error("message listener error:",e);
		}
	}

	@Override
	public void onException(Exception e) {
		// TODO Auto-generated method stub
		
	}

}
